package com.itheima.app.controller;

import com.itheima.app.manager.MovementMananger;
import com.itheima.domain.mongo.Movement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
public class MovementController {

    @Autowired
    MovementMananger movementMananger;

    //保存动态
    @PostMapping("/movements")
    public ResponseEntity save(Movement movement, MultipartFile[] imageContent) throws IOException {
        return movementMananger.save(movement,imageContent);
    }

    //分页查询我的动态
    @GetMapping("/movements/all")
    public ResponseEntity findMyMovementByPage(
            Long userId,
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize){
        return movementMananger.findMyMovementByPage(userId,pageNum,pageSize);
    }


    //分页查询好友动态
    @GetMapping("/movements")
    public ResponseEntity findFriendMovementByPage(
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize){
        return movementMananger.findFriendMovementByPage(pageNum,pageSize);
    }

    //分页查询推荐动态
    @GetMapping("/movements/recommend")
    public ResponseEntity findRecommendMovementByPage(
            @RequestParam(value = "page",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10") Integer pageSize){
        return movementMananger.findRecommendMovementByPage(pageNum,pageSize);
    }

    //动态点赞
    @GetMapping("/movements/{publishId}/like")
    public ResponseEntity movementLike(@PathVariable String publishId){
        return movementMananger.movementLike(publishId);
    }

    //取消点赞
    @GetMapping("/movements/{publishId}/dislike")
    public ResponseEntity movementDislike(@PathVariable String publishId){
        return movementMananger.movementDislike(publishId);
    }

    //动态喜欢
    @GetMapping("/movements/{publishId}/love")
    public ResponseEntity movementLove(@PathVariable String publishId){
        return movementMananger.movementLove(publishId);
    }

    //取消喜欢
    @GetMapping("/movements/{publishId}/unlove")
    public ResponseEntity movementUnlove(@PathVariable String publishId){
        return movementMananger.movementUnlove(publishId);
    }

    @GetMapping("/movements/{publishId}")
    public ResponseEntity findById(@PathVariable String publishId){
        return movementMananger.findById(publishId);
    }

    @GetMapping("/comments")
    public ResponseEntity findCommentByPage(
            @RequestParam(value = "movementId")String publishId,
            @RequestParam(value = "page",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pagesize",defaultValue = "10")Integer pageSize){
       return movementMananger.findCommentByPage(publishId,pageNum,pageSize);
    }

    @PostMapping("/comments")
    public ResponseEntity saveMovementComment(@RequestBody Map<String,String> map){
        //1.获取动态id和评论内容
        String publishId = map.get("movementId");
        String content = map.get("comment");

        //2.调用manager完成保存操作
        return movementMananger.saveMovementComment(publishId,content);
    }
}
